<template>
	<xn-form-container
		:title="formData.id ? '编辑食堂管理' : '增加食堂管理'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<!--			<a-form-item label="所属组织：" name="orgId">-->
			<!--				<a-tree-select-->
			<!--					v-model:value="formData.orgId"-->
			<!--					class="xn-wd"-->
			<!--					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"-->
			<!--					placeholder="请选择组织"-->
			<!--					allow-clear-->
			<!--					tree-default-expand-all-->
			<!--					:tree-data="treeData"-->
			<!--					:field-names="{-->
			<!--						children: 'children',-->
			<!--						label: 'name',-->
			<!--						value: 'id'-->
			<!--					}"-->
			<!--					selectable="false"-->
			<!--					tree-line-->
			<!--				/>-->
			<!--			</a-form-item>-->
			<a-form-item label="食堂名称：" name="canteenName">
				<a-input v-model:value="formData.canteenName" placeholder="请输入食堂名称" allow-clear />
			</a-form-item>
			<a-form-item label="食堂午餐价格：" name="canteenLunchPrice">
				<a-input v-model:value="formData.canteenLunchPrice" placeholder="请输入食堂午餐价格" allow-clear />
			</a-form-item>
			<a-form-item label="食堂晚餐价格：" name="canteenDinnerPrice">
				<span style="color: red"> 当价格为0，则表示该食堂不提供晚餐服务</span>
				<a-input v-model:value="formData.canteenDinnerPrice" placeholder="请输入食堂晚餐价格" allow-clear />
			</a-form-item>
			<a-form-item label="食堂价格是否可修改：" name="canteenPriceModifyFlag">
				<a-radio-group
					v-model:value="formData.canteenPriceModifyFlag"
					placeholder="请选择食堂价格是否可修改"
					:options="canteenPriceModifyFlagOptions"
				/>
			</a-form-item>
			<a-form-item label="午餐开始时间：" name="lunchBeginTime">
				<a-date-picker
					v-model:value="formData.lunchBeginTime"
					value-format="HH:mm:ss"
					show-time
					placeholder="请选择午餐开始时间"
					style="width: 100%"
					:picker="'time'"
				/>
			</a-form-item>
			<a-form-item label="午餐结束时间：" name="lunchEndTime">
				<a-date-picker
					v-model:value="formData.lunchEndTime"
					value-format="HH:mm:ss"
					show-time
					placeholder="请选择午餐结束时间"
					style="width: 100%"
					:picker="'time'"
				/>
			</a-form-item>

			<a-form-item label="晚餐开始时间：" name="dinerBeginTime">
				<a-date-picker
					v-model:value="formData.dinerBeginTime"
					value-format="HH:mm:ss"
					show-time
					placeholder="请选择晚餐开始时间"
					style="width: 100%"
					:picker="'time'"
				/>
			</a-form-item>
			<a-form-item label="晚餐结束时间：" name="dinerEndTime">
				<a-date-picker
					v-model:value="formData.dinerEndTime"
					value-format="HH:mm:ss"
					show-time
					placeholder="请选择晚餐结束时间"
					style="width: 100%"
					:picker="'time'"
				/>
			</a-form-item>
			<a-form-item label="商户号：" name="merchantId">
				<a-input v-model:value="formData.merchantId" placeholder="请输入商户号" allow-clear />
			</a-form-item>
			<a-form-item label="商户私钥：" name="merchantPrivateKey">
				<xn-upload v-model:value="formData.merchantPrivateKey" uploadMode="file" uploadResultType="id" />
			</a-form-item>
			<a-form-item label="商户私钥密码：" name="merchantPrivateKeyPassword">
				<a-input v-model:value="formData.merchantPrivateKeyPassword" placeholder="请输入商户私钥密码" allow-clear />
			</a-form-item>
			<a-form-item label="审批通知人：" name="directorId">
				<xn-user-selector
					:org-tree-api="selectorApiFunction.orgTreeApi"
					:user-page-api="selectorApiFunction.userPageApi"
					:radio-model="true"
					v-model:value="formData.directorId"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizCanteenForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizCanteenApi from '@/api/biz/bizCanteenApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	// import { convertUrl } from '@/utils/apiAdaptive'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const canteenPriceModifyFlagOptions = ref([])
	// 定义机构元素
	const treeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		canteenPriceModifyFlagOptions.value = tool.dictList('COMMON_SWITCH')
		// 获取机构树并加入顶级
		bizOrgApi.orgTreeSelector().then((res) => {
			treeData.value = res.map((node) => ({
				...node,
				disabled: true
			}))
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		// orgId: [required('请选择组织')],
		canteenName: [required('请输入食堂名称')],
		canteenPriceModifyFlag: [required('请选择食堂价格是否可修改')],
		lunchBeginTime: [required('请选择午餐开始时间')],
		lunchEndTime: [required('请选择午餐结束时间')],
		dinerBeginTime: [required('请选择晚餐开始时间')],
		dinerEndTime: [required('请选择晚餐结束时间')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizCanteenApi
					.bizCanteenSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 传递设计器需要的API
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return bizOrgApi.orgTreeSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userPageApi: (param) => {
			return bizOrgApi.orgUserSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
